<?php
class FgAction extends LoginAction
{
	public function brand ()
	{
		$id = (int) getGet('id');
		$this->setData($id);
		//获取商品信息
		$FloorBrandGoodModel = D('FloorBrandGood');
		$fb = $this->__get('fb');
		if ($fb) {
			$good = $FloorBrandGoodModel->getFloorBrandGood($fb['mid'], $fb['fid'], $fb['bid']);
			//TODO 获取对应活动
			$this->assign('good', $good['data']);
			$this->assign('page', $good['page']);
		}
		$this->display();
	}
	/**
	 * 执行简单的商品关联处理
	 * @author Ice iceinto@mallog.com.cn
	 */
	public function add ()
	{
		$id = (int) getGet('id');
		//获取当前商品列表中不存在的商品
		$this->setData($id, true);
		if (isPost()) {
			$data = $this->getPostData();
			$fbgModel = D('FloorBrandGood');
			$notice = $fbgModel->fbgAddMore($data);
			$this->noticeMessage($notice);
		}
		$this->display();
	}
	/**
	 * 修改商品详细
	 * @author Ice iceinto@mallog.com.cn
	 */
	public function edit ()
	{
		$id = (int) getGet('id');
		if (isPost()) {
			$data = array();
			$data['g_now_price'] = floatval(getPost('gNowPrice'));
			$data['g_color'] = getPost('gColor');
			$data['g_hot'] = intval(getPost('gHot'));
			$data['g_id'] = $id;
			$data['status'] = getPost('status');
			$FloorBrandGoodModel = M('FloorBrandGood');
			$status = $FloorBrandGoodModel->save($data);
			if ($status) {
				$this->successMessage('修改成功！');
			} else {
				$this->errorMessage('修改失败！');
			}
		}
		$map = array();
		$map['g_id'] = $id;
		$this->setBaseData('FloorBrandGood', $map, 'floorBrandGood');
		$floorBrandGood = $this->get('floorBrandGood');
		$this->setMarketData($floorBrandGood['g_m_id']);
		$this->setFloorData($floorBrandGood['g_f_id']);
		$this->setBrandData($floorBrandGood['g_b_id']);
		unset($map);
		$map['g_id'] = $floorBrandGood['g_g_id'];
		$this->setBaseData('Good', $map, 'good');
		$good = $this->get('good');
		$imageUrl = $this->getManagedUrl($good['g_image'], 'good');
		$this->assign('imageUrl', $imageUrl);
		$this->display();
	}
	/**
	 * 获取提交数据
	 * @author Ice iceinto@mallog.com.cn
	 */
	private function getPostData ()
	{
		$data = array();
		$data['mid'] = getPost('mId');
		$data['fid'] = getPost('fId');
		$data['bid'] = getPost('bId');
		$data['tid'] = getPost('btId');
		$data['gid'] = getPost('good');
		//判断必要性
		foreach ($data as $k => $d) {
			if (! $d) {
				$this->error($k . '不能为空！');
			}
		}
		return $data;
	}
	/**
	 * 设置基本参数
	 * @param Int $id 标识
	 * @author Ice iceinto@mallog.com.cn
	 */
	private function setData ($id, $isSetGood = false)
	{
		$fb = $this->setFbData($id);
		$this->setBrandData($fb['bid']);
		$this->setMarketData($fb['mid']);
		$this->setFloorData($fb['fid']);
		if ($isSetGood) {
			//不获取已经存在的
			$goods = $this->setGoodData($fb);
			foreach ($goods as $k => $g) {
				$imageUrl = $this->getManagedUrl($g['image'], 'good');
				$goods[$k]['image_url'] = $imageUrl;
			}
			$this->assign('goods', $goods);
		}
	}
	/**
	 * 设置品牌详细信息
	 * @param Int $id 品牌ID
	 * @author Ice iceinto@mallog.com.cn
	 */
	private function setBrandData ($id)
	{
		$map = array();
		$map['id'] = $id;
		$brand = $this->setBaseData('Brand', $map, 'brand');
		return $brand;
	}
	/**
	 * 设置商品列表信息
	 * @param Array $fb 楼层品牌
	 * @author Ice iceinto@mallog.com.cn
	 */
	private function setGoodData ($fb)
	{
		$map = array();
		//获取当前品牌所有商品数量
		$map['bid'] = $fb['bid'];
		$count = M('Good')->where($map)->count();
		if (! $count) {
			$this->error('当前品牌库中不存在对应商品信息！');
		}
		//获取当前商场楼层品牌中存在的商品数量
		$map['mid'] = $fb['mid'];
		$map['fid'] = $fb['fid'];
		$nowCount = M('FloorBrandGood')->where($map)->count();
		$this->assign('goodCount', $count);
		$this->assign('goodNowCount', $nowCount);
		if ($nowCount == $count) {
			$this->error('当前品牌商品已经全部存在！');
		} else {
			//排除条件
			/**SELECT * FROM m_good WHERE g_id NOT IN
			 (SELECT g_g_id FROM m_floor_brand_good WHERE g_m_id =2 AND g_f_id = 18 AND g_b_id =57 )
			*/
			//过滤掉已经存在的商品
			$sql = 'SELECT name,id,image FROM m_good WHERE id NOT IN
			(SELECT gid FROM m_floor_brand_good WHERE mid =' .
			$map['mid'] . ' AND fid = ' . $map['fid'] . ' AND bid =' .
			$map['bid'] . ' ) AND bid = ' . $map['bid'];
			$goods = M('Good')->query($sql);
			return $goods;
		}
	}
	/**
	 * 设置楼层品牌信息
	 * @param Int $id 楼层ID
	 * @author Ice iceinto@mallog.com.cn
	 */
	private function setFbData ($id)
	{
		$map = array();
		$map['id'] = $id;
		return $this->setBaseData('FloorBrand', $map, 'fb');
	}
	/**
	 * 设置商场基本信息
	 * @param Int $id 商场ID
	 * @author Ice iceinto@mallog.com.cn
	 */
	private function setMarketData ($id)
	{
		$map = array();
		$map['id'] = $id;
		return $this->setBaseData('Market', $map, 'market');
	}
	/**
	 * 设置楼层基本信息
	 * @param Int $id 楼层ID
	 * @author Ice iceinto@mallog.com.cn
	 */
	private function setFloorData ($id)
	{
		$map = array();
		$map['id'] = $id;
		return $this->setBaseData('Floor', $map, 'floor');
	}
}
?>